//
//  BK_My_Cup_NoUsed_collectionCell.m
//  bestkeep
//
//  Created by TOPTEAM on 2017/2/9.
//  Copyright © 2017年 utouu. All rights reserved.
//


#import "BK_My_Cup_NoUsed_collectionCell.h"

#import "BK_my_couponCommon.h"
#import "BKMyCommon.h"

#import "BK_my_CouponModel.h"
#import "BKRefreshControl.h"
#import "MJRefresh.h"
#import "Mediator+BKGD.h"
#import "Mediator+BKClassify.h"
#import "BK_my_CouponTextField.h"
#import "ExceptionView.h"

#import "BK_My_Cup_NoUsed_tabVCell.h"
#import "BK_my_CouponNoDataView.h"

@interface BK_My_Cup_NoUsed_collectionCell ()

@property (nonatomic, strong) ExceptionView *exceptionView;
@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, strong) NSString *useType;
@property (nonatomic,strong)BK_my_CouponNoDataView *noUse_noDataView;

@end


@implementation BK_My_Cup_NoUsed_collectionCell
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        self.backgroundColor = RGBCOLOR(242, 242, 242, 1);
        [self setProperty];
        [self initView];

        [self checkNetWorking];
        
    }
    return self;
}


#pragma mark=============setProperty=============
-(void)setProperty{
    self.pageSize = 20;
    self.currentPage = 1;
    self.useType=@"1";
}
#pragma mark=============initView=============
- (void)initView{
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64-36) style:UITableViewStylePlain];
    
    [self addSubview:_tableView];
    
    _tableView.backgroundColor=RGBCOLOR(242, 242, 242, 1);
    
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
    v.backgroundColor = [UIColor clearColor];
    _tableView.tableFooterView = v;
    
    
    
    _noUse_noDataView = [[BK_my_CouponNoDataView alloc] initWithFrame:CGRectMake(0, 46, ScreenWidth, ScreenHeight-64-46)];
    _noUse_noDataView.backgroundColor = RGBCOLOR(242, 242, 242, 1);
    [self addSubview:_noUse_noDataView];
    _noUse_noDataView.hidden=YES;
    
    
}

#pragma mark=============loadData=============
-(void)BK_My_Cup_NoUsed_loadData{
    [self dataWithType:self.useType pageSize:[NSString stringWithFormat:@"%@", @(self.pageSize)] pageno:[NSString stringWithFormat:@"%@", @(self.currentPage)]];
}


//第section分区一共有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}


//创建第section分区第row行的UITableViewCell对象(indexPath包含了section和row)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    BK_My_Cup_NoUsed_tabVCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BK_My_Cup_NoUsed_tabVCell"];
    if (cell == nil) {
        cell = [[BK_My_Cup_NoUsed_tabVCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BK_My_Cup_NoUsed_tabVCell"];
    }
    cell.usedModel = self.dataArr[indexPath.row];
    return cell;

}

#pragma mark - tableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 101;
}




#pragma mark - TableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

#pragma mark - tableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.001;
}

#pragma mark===================Footer===================
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//
//}

#pragma mark===================Header===================
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

//加载数据方法
- (void)dataWithType:(NSString *)useType pageSize:(NSString *)page_size pageno:(NSString *)page_no {

    [BKIndicationView showInView:self];
    [BK_my_couponCommon getVoucherShopping:useType pagesize:page_size pageno:page_no callback:^(id obj, NSError *error) {
        if (error) {
            if(!self.exceptionView && error) {
                self.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeServerError];

                ExceptionViewAction *retryAction = [ExceptionViewAction actionWithType:ExceptionCodeTypeServerError handler:^(ExceptionViewAction *action) {
                    [BKIndicationView showInView:self];
                    [self BK_My_Cup_NoUsed_loadData];
                }];
                
                [self.exceptionView addAction:retryAction];
                [self.exceptionView showInView:self];

                [BKIndicationView dismiss];
            } else {
                [BKIndicationView dismiss];
            }
        } else {
            if(self.exceptionView) {
                [self.exceptionView dismiss];
                self.exceptionView = nil;
            }
            [BKIndicationView dismiss];
            NSMutableArray *mArr = [NSMutableArray array];
            NSInteger total = 0;
            NSDictionary *dict = obj;
            NSArray *arr = dict[@"rows"];

            for (NSDictionary *subDict in arr) {
                BK_my_CouponModel *model = [self dictionary:subDict dic:dict integer:total];
                [mArr addObject:model];
            }

            self.dataArr = mArr;
            
            if (mArr.count == 0) {
                _noUse_noDataView.hidden=NO;
            } else {
                _noUse_noDataView.hidden=YES;
            }
            
      }
        [_tableView.mj_footer endRefreshing];
        [_tableView.mj_header endRefreshing];
        [_tableView reloadData];
    }];
}


- (BK_my_CouponModel *)dictionary:(NSDictionary *)subDict dic:(NSDictionary *)dict integer:(NSInteger)total {
    BK_my_CouponModel *model = [[BK_my_CouponModel alloc]init];
    model.couponId  = subDict[@"couponId"];
    model.goodsId = subDict[@"goodsId"];
    model.couponTypeStr = subDict[@"couponTypeStr"];
    model.goodsNo = subDict[@"goodsNo"];
    model.invalidTimeStr = subDict[@"invalidTimeStr"];
    model.reduceCost = subDict[@"reduceCost"];
    model.remainingWarning = subDict[@"remainingWarning"];
    model.viewGoodsInfoStr = subDict[@"viewGoodsInfoStr"];
    model.viewCategoryInfoStr = subDict[@"viewCategoryInfoStr"];
    model.status = [NSString stringWithFormat:@"%@",subDict[@"status"]];
    model.pagesize =[NSString stringWithFormat:@"%@", dict[@"pagesize"]];
    model.pageno = [NSString stringWithFormat:@"%@",dict[@"pageno"]];
    model.total = [NSString stringWithFormat:@"%@",dict[@"total"]];
    model.use_Type = [NSString stringWithFormat:@"%@",dict[@"useType"]];
    model.sprticific = [subDict[@"sprticific"] integerValue];
    model.categoryId = subDict[@"categoryId"];
    total = [dict[@"total"] doubleValue];
    return model;
}


-(void)checkNetWorking {
    RequestReachabilityStatus status = [RequestManager reachabilityStatus];
    switch (status) {
        case RequestReachabilityStatusReachableViaWiFi:
        case RequestReachabilityStatusReachableViaWWAN: {
            [self BK_My_Cup_NoUsed_loadData];
        }
            break;
            
        default: {
            [BKIndicationView dismiss];
            self.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeNoNetwork];
            ExceptionViewAction *action = [ExceptionViewAction actionWithType:ExceptionCodeTypeNoNetwork handler:^(ExceptionViewAction *action) {
                [self.exceptionView dismiss];
                self.exceptionView = nil;
                [self checkNetWorking];
            }];
            [self.exceptionView addAction:action];
            [self.exceptionView showInView:self];
            
        }
            break;
    }
}

@end



























